#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include "resource.h"

enum TIMETYPE {SYSTEM,LOCAL};

HINSTANCE hInst;
HANDLE hFile;

FILETIME ftCreate, ftAccess, ftWrite;
SYSTEMTIME stCreate, stAccess, stWrite;
SYSTEMTIME ltCreate, ltAccess, ltWrite;

void FileTime2SystemTime()
{
    FileTimeToSystemTime(&ftCreate,&stCreate);
    FileTimeToSystemTime(&ftAccess,&stAccess);
    FileTimeToSystemTime(&ftWrite,&stWrite);
};

void SystemTime2FileTime()
{
    SystemTimeToFileTime(&stCreate,&ftCreate);
    SystemTimeToFileTime(&stAccess,&ftAccess);
    SystemTimeToFileTime(&stWrite,&ftWrite);
};

void SystemTime2LocalTime()
{
    SystemTimeToTzSpecificLocalTime(NULL, &stCreate, &ltCreate);
    SystemTimeToTzSpecificLocalTime(NULL, &stAccess, &ltAccess);
    SystemTimeToTzSpecificLocalTime(NULL, &stWrite, &ltWrite);
};

void LocalTime2SystemTime()
{
    TzSpecificLocalTimeToSystemTime(NULL, &ltCreate, &stCreate);
    TzSpecificLocalTimeToSystemTime(NULL, &ltAccess, &stAccess);
    TzSpecificLocalTimeToSystemTime(NULL, &ltWrite, &stWrite);
};



void Read(HWND hwndDlg,TIMETYPE type)//bool isLocal
{
    bool t = (type == SYSTEM);
    SYSTEMTIME &C = (t?stCreate:ltCreate);
    SYSTEMTIME &A = (t?stAccess:ltAccess);
    SYSTEMTIME &W = (t?stWrite:ltWrite);

    C.wHour = GetDlgItemInt(hwndDlg,IDC_EDIT_A_h,0,FALSE);
    C.wMinute = GetDlgItemInt(hwndDlg,IDC_EDIT_A_m,0,FALSE);
    C.wSecond = GetDlgItemInt(hwndDlg,IDC_EDIT_A_s,0,FALSE);
    C.wMilliseconds = GetDlgItemInt(hwndDlg,IDC_EDIT_A_n,0,FALSE);
    C.wDay = GetDlgItemInt(hwndDlg,IDC_EDIT_A_d,0,FALSE);
    C.wMonth = GetDlgItemInt(hwndDlg,IDC_EDIT_A_M,0,FALSE);
    C.wYear = GetDlgItemInt(hwndDlg,IDC_EDIT_A_y,0,FALSE);

    A.wHour = GetDlgItemInt(hwndDlg,IDC_EDIT_B_h,0,FALSE);
    A.wMinute = GetDlgItemInt(hwndDlg,IDC_EDIT_B_m,0,FALSE);
    A.wSecond = GetDlgItemInt(hwndDlg,IDC_EDIT_B_s,0,FALSE);
    A.wMilliseconds = GetDlgItemInt(hwndDlg,IDC_EDIT_B_n,0,FALSE);
    A.wDay = GetDlgItemInt(hwndDlg,IDC_EDIT_B_d,0,FALSE);
    A.wMonth = GetDlgItemInt(hwndDlg,IDC_EDIT_B_M,0,FALSE);
    A.wYear = GetDlgItemInt(hwndDlg,IDC_EDIT_B_y,0,FALSE);

    W.wHour = GetDlgItemInt(hwndDlg,IDC_EDIT_C_h,0,FALSE);
    W.wMinute = GetDlgItemInt(hwndDlg,IDC_EDIT_C_m,0,FALSE);
    W.wSecond = GetDlgItemInt(hwndDlg,IDC_EDIT_C_s,0,FALSE);
    W.wMilliseconds = GetDlgItemInt(hwndDlg,IDC_EDIT_C_n,0,FALSE);
    W.wDay = GetDlgItemInt(hwndDlg,IDC_EDIT_C_d,0,FALSE);
    W.wMonth = GetDlgItemInt(hwndDlg,IDC_EDIT_C_M,0,FALSE);
    W.wYear = GetDlgItemInt(hwndDlg,IDC_EDIT_C_y,0,FALSE);
};

void Write(HWND hwndDlg,TIMETYPE type)
{
    bool t = (type == SYSTEM);
    SYSTEMTIME &C = (t?stCreate:ltCreate);
    SYSTEMTIME &A = (t?stAccess:ltAccess);
    SYSTEMTIME &W = (t?stWrite:ltWrite);

    SetDlgItemInt(hwndDlg,IDC_EDIT_A_h,C.wHour,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_A_m,C.wMinute,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_A_s,C.wSecond,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_A_n,C.wMilliseconds,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_A_d,C.wDay,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_A_M,C.wMonth,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_A_y,C.wYear,FALSE);

    SetDlgItemInt(hwndDlg,IDC_EDIT_B_h,A.wHour,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_B_m,A.wMinute,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_B_s,A.wSecond,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_B_n,A.wMilliseconds,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_B_d,A.wDay,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_B_M,A.wMonth,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_B_y,A.wYear,FALSE);

    SetDlgItemInt(hwndDlg,IDC_EDIT_C_h,W.wHour,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_C_m,W.wMinute,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_C_s,W.wSecond,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_C_n,W.wMilliseconds,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_C_d,W.wDay,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_C_M,W.wMonth,FALSE);
    SetDlgItemInt(hwndDlg,IDC_EDIT_C_y,W.wYear,FALSE);
};

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_PAINT:
            break;

        case WM_CREATE:
            return TRUE;

        case WM_INITDIALOG:
            CheckDlgButton(hwndDlg, IDC_CHECKBOX, BST_CHECKED);
            Write(hwndDlg,LOCAL);
            return TRUE;

        case WM_CLOSE:
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDC_CHECKBOX:
                    if(SendDlgItemMessage(hwndDlg, IDC_CHECKBOX, BM_GETCHECK, 0, 0) == BST_CHECKED)
                    {
                        SendDlgItemMessage(hwndDlg, IDC_CHECKBOX, BM_SETCHECK,(WPARAM)BST_UNCHECKED, 0);
                        Read(hwndDlg,LOCAL);
                        LocalTime2SystemTime();
                        Write(hwndDlg,SYSTEM);
                    }
                    else
                    {
                        SendDlgItemMessage(hwndDlg, IDC_CHECKBOX, BM_SETCHECK,(WPARAM)BST_CHECKED, 0);
                        Read(hwndDlg,SYSTEM);
                        SystemTime2LocalTime();
                        Write(hwndDlg,LOCAL);
                    }
                    return TRUE;

                case IDC_BTN_OK:
                    EndDialog(hwndDlg, 0);

                    if(SendDlgItemMessage(hwndDlg, IDC_CHECKBOX, BM_GETCHECK, 0, 0) == BST_CHECKED)
                    {
                        Read(hwndDlg,LOCAL);
                        LocalTime2SystemTime();
                    } else
                        Read(hwndDlg,SYSTEM);

                    SystemTime2FileTime();

                    if (SetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite) == 0)
                        MessageBox(hwndDlg, "No se pudo cambiar", "Error!", MB_ICONERROR);

                    return TRUE;
            }
            break;

		case WM_CTLCOLORBTN:
            SetBkMode((HDC)wParam, TRANSPARENT);
            break;

		case WM_CTLCOLORSTATIC:
			HDC hdc = (HDC)wParam;
			HWND hWnd = (HWND)lParam;
			if(hWnd == (HWND)IDC_CHECKBOX)
			{
				SetBkMode(hdc,TRANSPARENT);
				return 0;
			}
			break;
        /*case WM_CTLCOLORSTATIC:
			SetBkMode((HDC)wParam, TRANSPARENT);
			if(lParam != IDC_CHECKBOX)
				return (LONG)((HBRUSH)(CreateSolidBrush(RGB(255, 255, 255))));
			else
				return (LONG)((HBRUSH)(CreateSolidBrush(RGB(255, 0, 0))));
            break;*/
        
    }

    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;

    if(*lpCmdLine == '"')
	{
        *(lpCmdLine + (strlen(lpCmdLine)-1)) = '\0';
		lpCmdLine++;
	}

    hFile = CreateFile(lpCmdLine, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL,OPEN_EXISTING, 0, NULL);

    if(hFile == INVALID_HANDLE_VALUE)
    {
        MessageBox(NULL, "Error al abrir el archivo.", "Error!", MB_ICONERROR);
        return GetLastError();
    }

    if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
    {
        MessageBox(NULL, "Error al obtener fechas del archivo.", "Error!", MB_ICONERROR);
        return GetLastError();
    }

    FileTime2SystemTime();
    SystemTime2LocalTime();

    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, DialogProc);
}
